{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D\n"
     ]
    }
   ],
   "source": [
    "score = int(input('请输入一个整数') )\n",
    "if score>100 or score<0:\n",
    "    print('wrong score.must between 0 and 100.')\n",
    "elif score>=90:\n",
    "    print('A')\n",
    "elif score>=80:\n",
    "    print('B')\n",
    "elif score>=70:\n",
    "    print('C')\n",
    "elif score>=60:\n",
    "    print('D')\n",
    "else:\n",
    "    print('F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D\n"
     ]
    }
   ],
   "source": [
    "score=int(input('请输入一个整数'))\n",
    "degree='DCBAAF'                     #[90,99]区间和100都对应A\n",
    "if score >100 or score <0:\n",
    "    print('wrong score.must between 0 and 100.')\n",
    "else:\n",
    "    index=( score-60)//10\n",
    "    if index>=0:                #这里对应60分以上的成绩\n",
    "        print(degree[index])\n",
    "    else:\n",
    "        print(degree[-1])       #60分以下，对应F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "6\n",
      "11\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[11, 13, 12, 15, 10, 19, 18, 14, 17, 16, 4, 3, 9, 2, 6, 8, 0, 7, 5, 1]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> f = lambda x, y, z: x + y + z     #也可以给lambda表达式起个名字\n",
    ">>> print(f(1, 2, 3))                 #把lambda表达式当作函数使用\n",
    "6\n",
    ">>> g = lambda x, y=2, z=3: x + y + z #支持默认值参数\n",
    ">>> print(g(1))\n",
    "6\n",
    ">>> print(g(2, z=4, y=5)) #调用时使用关键参数\n",
    "11\n",
    ">>> L = [1, 2, 3, 4, 5]\n",
    ">>> list(map(lambda x: x + 10, L)) #lambda表达式作为函数参数\n",
    "[11, 12, 13, 14, 15]\n",
    ">>> data = list(range(20))\n",
    ">>> import random\n",
    ">>> random.shuffle(data) #随机打乱顺序\n",
    ">>> data\n",
    "[4, 3, 9, 2, 6, 8, 0, 7, 5, 1, 11, 13, 12, 15, 10, 19, 18, 14, 17, 16]\n",
    ">>> data.sort(key=lambda x: len(str(x)))\n",
    "                                         #使用lambda表达式指定排序规则\n",
    "                                         #按所有元素转换为字符串后的长度排序\n",
    ">>> data\n",
    "[4, 3, 9, 2, 6, 8, 0, 7, 5, 1, 11, 13, 12, 15, 10, 19, 18, 14, 17, 16]\n",
    ">>> data.sort(key=lambda x: len(str(x)), reverse=True)\n",
    "                                         #reverse=True表示降序排序\n",
    ">>> data\n",
    ">>> data\n",
    "[11, 13, 12, 15, 10, 19, 18, 14, 17, 16, 4, 3, 9, 2, 6, 8, 0, 7, 5, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 * 1 = 1  \n",
      "1 * 2 = 2  2 * 2 = 4  \n",
      "1 * 3 = 3  2 * 3 = 6  3 * 3 = 9  \n",
      "1 * 4 = 4  2 * 4 = 8  3 * 4 = 12  4 * 4 = 16  \n",
      "1 * 5 = 5  2 * 5 = 10  3 * 5 = 15  4 * 5 = 20  5 * 5 = 25  \n",
      "1 * 6 = 6  2 * 6 = 12  3 * 6 = 18  4 * 6 = 24  5 * 6 = 30  6 * 6 = 36  \n",
      "1 * 7 = 7  2 * 7 = 14  3 * 7 = 21  4 * 7 = 28  5 * 7 = 35  6 * 7 = 42  7 * 7 = 49  \n",
      "1 * 8 = 8  2 * 8 = 16  3 * 8 = 24  4 * 8 = 32  5 * 8 = 40  6 * 8 = 48  7 * 8 = 56  8 * 8 = 64  \n",
      "1 * 9 = 9  2 * 9 = 18  3 * 9 = 27  4 * 9 = 36  5 * 9 = 45  6 * 9 = 54  7 * 9 = 63  8 * 9 = 72  9 * 9 = 81  \n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(j,'*',i,'=',i*j,end='  ')\n",
    "    print()  #打印空行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "for n in range(100, 1 - 1):\n",
    "    if n % 2 == 0:\n",
    "        continue\n",
    "    for i in range(3, int(n**0.5) + 1, 2):\n",
    "        if n % i == 0:\n",
    "            # 结束内循环\n",
    "            break\n",
    "    else:\n",
    "        print(n)\n",
    "        # 结束外循环\n",
    "        break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "只能输入yes或no\n",
      "88.0\n"
     ]
    }
   ],
   "source": [
    "numbers = []\n",
    "while True:\n",
    "    x = input(\"请输入一个成绩：\")\n",
    "    # 异常处理结构，用来保护用户只能输入实数，可自行增加代码限制实数范围\n",
    "    try:\n",
    "        # 先把x转换成实数，然后追加到列表numbers尾部\n",
    "        numbers.append(float(x))\n",
    "    except:\n",
    "        print('不是合法成绩')\n",
    "\n",
    "    # 下面的循环用来限制用户只能输入任意大小写的“yes”或“no”\n",
    "    while True:\n",
    "        flag = input('继续输入吗？(yes/no)：').lower()\n",
    "        if flag not in ('yes', 'no'):\n",
    "            print('只能输入yes或no')\n",
    "        else:\n",
    "            break\n",
    "    if flag == 'no':\n",
    "        break\n",
    "\n",
    "    # 计算平均分\n",
    "    print(sum(numbers) / len(numbers))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
